﻿using People;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Xml;
using System.Text;
using System.Collections.Generic;
using System.Xml.Linq;

namespace Test.XMLProcessing
{
    [TestClass()]
    public class PeopleTest
    {
        [TestMethod()]
        [DeploymentItem("People.exe")]
        public void WritingResultXMLTest()
        {
            Dictionary<string, int> languages = new Dictionary<string, int>();
            languages.Add("bg-BG", 3);
            languages.Add("en-US", 5);
            languages.Add("au-AU", 2);
            languages.Add("ca-CA", 1);
            languages.Add("ru-RU", 4);
            People_Accessor.Languages = languages;
            string fileName = "../../peopleByLanguagesTest.xml";
            Encoding encoding = Encoding.GetEncoding("windows-1251");
            XmlTextWriter writer = new XmlTextWriter(fileName, encoding);
            People_Accessor.WritingResultXML(writer);

            XmlReader reader = XmlReader.Create("../../peopleByLanguagesTest.xml");
            using (reader)
            {
                while (reader.Read())
                {
                    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "language"))
                    {
                        string language = reader.GetAttribute(0);
                        Assert.IsTrue(languages.ContainsKey(language));
                        CheckingNumberOfPeople(languages, reader, language);
                    }
                }
            }
        }

        private static void CheckingNumberOfPeople(Dictionary<string, int> languages, XmlReader reader, string language)
        {
            while (reader.Read())
            {
                if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "numberOfPeople"))
                {
                    string number = reader.ReadInnerXml();
                    Assert.AreEqual(languages[language].ToString(), number);
                }
                if ((reader.NodeType == XmlNodeType.EndElement) && (reader.Name == "language"))
                {
                    break;
                }
            }
        }


        [TestMethod()]
        [DeploymentItem("People.exe")]
        public void GetingDataFromXMLTest()
        {
            Dictionary<string, int> languages = new Dictionary<string, int>();
            languages.Add("bg-BG", 3);
            languages.Add("en-US", 5);
            languages.Add("au-AU", 2);
            languages.Add("ca-CA", 1);
            languages.Add("ru-RU", 4);

            CreatingXML(languages);

            XmlReader reader = XmlReader.Create("../../peopleTest.xml");
            People_Accessor.GetingDataFromXML(reader);
            foreach (var item in languages)
            {
                Assert.AreEqual(languages[item.Key], People_Accessor.Languages[item.Key]);
            }
        }

        private static void CreatingXML(Dictionary<string, int> languages)
        {
            string fileName = "../../peopleTest.xml";
            Encoding encoding = Encoding.GetEncoding("windows-1251");
            XmlTextWriter writer = new XmlTextWriter(fileName, encoding);
            using (writer)
            {
                writer.Formatting = Formatting.Indented;
                writer.IndentChar = '\t';
                writer.Indentation = 1;

                writer.WriteStartDocument();
                writer.WriteStartElement("companies");
                foreach (var item in languages)
                {
                    writer.WriteStartElement("company");
                    writer.WriteStartElement("people");
                    for (int i = 0; i < item.Value; i++)
                    {
                        writer.WriteStartElement("person");
                        writer.WriteStartElement("FirstName");
                        writer.WriteAttributeString("language", item.Key);
                        writer.WriteEndElement();
                        writer.WriteStartElement("LastName");
                        writer.WriteAttributeString("language", item.Key);
                        writer.WriteEndElement();
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                }
                writer.WriteEndDocument();
            }
        }
    }
}
